package me.leon.mylibs;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

import io.realm.Realm;
import io.realm.RealmQuery;
import me.leon.mylibs.DB.JsonBean;
import me.leon.mylibs.DB.Person;

public class ConstraitActivity extends AppCompatActivity {

    private TextView txt;
    private List<JsonBean> jsonBeans;
    private TextView text;
    private CityPickerHelper pickViewHelper;

    public static void start(Context context) {
        Intent starter = new Intent(context, ConstraitActivity.class);
//        starter.putExtra();
        context.startActivity(starter);
        RealmQuery<Person> where = Realm.getDefaultInstance().where(Person.class);
        if (where != null) {
            if (where.findAll() != null) {
                Realm.getDefaultInstance().executeTransactionAsync(realm -> realm.deleteAll());
            }
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_constrait);
        View root = findViewById(R.id.root);
        txt = findViewById(R.id.bt1);
        text = findViewById(R.id.tv2);
        text.setOnClickListener(v -> showProvice());
    }

    private void showProvice() {

        if (pickViewHelper == null) {
            pickViewHelper = new CityPickerHelper(this,true);
        }
        ShowPickerView();
    }


    public void queryDB(View view) {
        List<String> list = new ArrayList<>();
        List<List<String>> list2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            list.add("item1 " + i);
            List<String> tmp = new ArrayList<>();
            list2.add(tmp);
            for (int i1 = i + 1; i1 < 6; i1++) {
                tmp.add("item2 " + i1);
            }

        }
        OptionsPickerView pickerView = new OptionsPickerView.Builder(this, (i0, i1, i2, v) -> {
            Toast.makeText(this, list2.get(i0).get(i1), Toast.LENGTH_SHORT).show();
        })
                .setSubmitText("确定")
                .setCancelText("取消")
//                .setTitleText("城市选择")
                .setSubCalSize(16)//确定和取消文字大小
                .setTitleSize(20)//标题文字大小
//                .setTitleColor(Color.BLACK)
                .setSubmitColor(0xff333434)
                .setCancelColor(0xff5C5E5E)
                .setTitleBgColor(Color.WHITE)
                .setBgColor(0xFFFFFFFF)
                .setContentTextSize(18)
//                .setLinkage(false)//设置是否联动，默认true
//                .setLabels("省", "市", "区")//设置选择的三级单位
                .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(0, 1, 1)  //设置默认选中项
//                .setOutSideCancelable(true)//点击外部dismiss default true
//                .isDialog(true)//是否显示为对话框样式
                .build();

        pickerView.setPicker(list, list2);
        pickerView.show();


    }


    private List<JsonBean> options1Items = new ArrayList<>();
    private List<List<String>> options2Items = new ArrayList<>();
    private List<List<List<String>>> options3Items = new ArrayList<>();

    private void ShowPickerView() {// 弹出选择器

        OptionsPickerView pvOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                String tx = pickViewHelper.getOptions1Items().get(options1).getPickerViewText() +
                        pickViewHelper.getOptions2Items().get(options1).get(options2) +
                        pickViewHelper.getOptions3Items().get(options1).get(options2).get(options3);

                text.setText(tx);
            }
        })

                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();

        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
        pvOptions.setPicker(pickViewHelper.getOptions1Items(), pickViewHelper.getOptions2Items(), pickViewHelper.getOptions3Items());//三级选择器
        pvOptions.show();
    }
}
